package com.bdth.weget.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Administrator on 2018/4/17.
 */
public class LogsUtil {
    public static void writeLog(String file,String log){
        String dir = "\\logs\\package\\";
        append(dir,file,log);
    }

    public static void append(File file, String log){
        try {
            FileWriter fw = new FileWriter(file, true);
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(log);
            bw.close();
            fw.close();
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    public static void append(String dir,String path, String log){
        File file = LogsUtil.createFile(dir,path);
        append(file,log);
    }

    public static File createFile(String dir,String path){
        String userPath = System.getProperty("user.dir");
        //判断配置文件是否存在不存在则创建
        File d = new File(userPath+dir);
        if(!d.exists()){
            d.mkdirs();
        }
        //初始化创建配置文件
        String filePath = userPath+dir+path;
        File logFile = new File(filePath);
        if(!logFile.exists()){
            try {
                logFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return logFile;
    }

    public static void main(String[] args) {
        createFile("\\aaa\\bb\\","aa.log");
    }
}
